from sqlalchemy import create_engine, Column, Integer, String, Text, Boolean
import os
from sqlalchemy.orm import declarative_base, sessionmaker
from contextlib import contextmanager

Base = declarative_base()


class Job(Base):
    __tablename__ = "jobs_zhilian"
    id = Column(Integer, primary_key=True)
    job_id = Column(String(50), comment="职位ID")
    job_name = Column(String(100), comment="职位名称")
    company = Column(String(100), comment="公司名称")
    city = Column(String(100), comment="所在城市")
    experience = Column(String(100), comment="工作经验要求")
    education = Column(String(100), comment="学历要求")
    description = Column(Text(), comment="职位描述")
    match_result = Column(Text(), comment="分析匹配结果")


MYSQL_HOST = os.getenv("MYSQL_HOST")
MYSQL_USER = os.getenv("MYSQL_USER")
MYSQL_PASSWORD = os.getenv("MYSQL_PASSWORD")
MYSQL_PORT = os.getenv("MYSQL_PORT")
MYSQL_DB = os.getenv("MYSQL_DB", "jobs")
# 创建引擎
engine = create_engine(
    f"mysql+pymysql://{MYSQL_USER}:{MYSQL_PASSWORD}@{MYSQL_HOST}/{MYSQL_DB}"
)
# 创建表
Base.metadata.create_all(engine)  #
Session = sessionmaker(bind=engine)


@contextmanager
def get_db_session():
    session = Session()
    try:
        yield session
        session.commit()
    except Exception as e:
        session.rollback()
        raise
    finally:
        session.close()
